package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.dao.AdRoleMapper;
import com.example.admin.dao.AdUserRoleMapper;
import com.example.admin.dao.UserMapper;
import com.example.admin.service.UserService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdRole;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.admin.pojos.AdUserRole;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.user.dtos.AddUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.utils.common.MD5Utils;
import com.usian.utils.common.SnowflakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdRoleMapper adRoleMapper;
    @Autowired
    private AdUserRoleMapper adUserRoleMapper;

    @Override
    public ResponseResult addUser(AddUser addUser) {
        AdUser apUser = new AdUser();
        BeanUtils.copyProperties(addUser,apUser);
        apUser.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        apUser.setLoginTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        Random random = new Random();
        int i = random.nextInt(9999);
        if (i<1000){
            i += 1000;
        }
        String salt = i + "aaa";
        String password = MD5Utils.encodeWithSalt(addUser.getPassword(), salt);
        apUser.setPassword(password);
        apUser.setStatus(1);
        apUser.setSalt(salt);
        try {
            userMapper .insert(apUser);
            QueryWrapper<AdRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_enable",0);
            queryWrapper.eq("name","用户");
            AdRole adRole = adRoleMapper.selectOne(queryWrapper);
            if (adRole==null){
                AdRole role = new AdRole();
                role.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
                role.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                role.setDescription("普通的操作");
                role.setIsEnable(0);
                role.setName("用户");
                adRoleMapper.insert(role);
                adRole.setId(role.getId());
            }
            AdUserRole role = new AdUserRole();
            role.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            role.setUserId(apUser.getId());
            role.setRoleId(adRole.getId());
            role.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            adUserRoleMapper.insert(role);
            return ResponseResult.okResult(200,"注册成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.errorResult(401,"注册失败");
    }

    @Override
    public ResponseResult loginUser(AdUserDto adUserDto) {
        System.out.println(
                adUserDto
        );
        QueryWrapper<AdUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        queryWrapper.eq("name",adUserDto.getName());
        List<AdUser> list = userMapper.selectList(queryWrapper);
        if (list.size()!=1){
            System.out.println(list);
            return ResponseResult.errorResult(400,"您的手机不存在,请在试试");
        }
        String md5 = MD5Utils.encodeWithSalt(adUserDto.getPassword(), list.get(0).getSalt());
        if (md5.equals(list.get(0).getPassword())){
            Map map = new HashMap();
            list.get(0).setSalt("");
            list.get(0).setPassword("");
            map.put("user",list.get(0));
            String token = AppJwtUtil.getToken(Long.parseLong(list.get(0).getId() + ""));
            map.put("token",token);
            return ResponseResult.okResult(map);
        }
        return ResponseResult.errorResult(503,"您登陆的密码不正确,请重新登录");
    }
}
